package com.bulemobi.activity.ac_user_centre;


import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.bulemobi.data.DataCentre;
import com.bulemobi.net.tsz.afinal.FinalHttp;
import com.bulemobi.net.tsz.afinal.http.AjaxCallBack;
import com.bulemobi.net.tsz.afinal.http.AjaxParams;
import com.bulemobi.urlCentre.UrlCentre;
import com.bulemobi.util.MD5Util;
import com.bulemobi.view.BaseActivity;
import com.bulemobi.view.titleView.ActionBarView;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import com.bulemobi.net.tsz.afinal.annotation.view.ViewInject;
import com.jsyh.hcb.R;
import org.json.JSONException;
import org.json.JSONObject;

import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zengys 添加车辆
 *         <p/>
 *         2014年9月18日 13:25:23
 */
public class AddCarActivity extends BaseActivity implements OnClickListener, ChoiceSeatType.GetSeatType {

    @ViewInject(id = R.id.actionBar)
    ActionBarView actionBar;
    @ViewInject(id = R.id.btn_present)
    Button btn_present;
    @ViewInject(id = R.id.et_card_number)
    EditText et_card_number;
    @ViewInject(id = R.id.et_engine_number)
    EditText et_engine_number;

    private ChoiceSeatType cst;
    private TextView dw;
//    @ViewInject(id = R.id.et_identity_number)
//    EditText et_identity_number;

    public static String markType = "02";
    public String carNo = "";
    public String engine_no = "";
    public String mobile = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_add_card);
        findActionBar();
        initView();
    }

    private void initView() {
        cst = new ChoiceSeatType(AddCarActivity.this, this);
        dw = (TextView) findViewById(R.id.xiaoche);
        dw.setOnClickListener(this);
        btn_present.setOnClickListener(this);
    }

    private void findActionBar() {
        //设置标题
        actionBar.title.setText(getResources().getString(R.string.add_car));
        //设置控件可见不可见
        //actionBar.button_right_icon1.setVisibility(View.VISIBLE);
        //设置背景图片
        //actionBar.button_right_icon1.setBackgroundResource(R.drawable.ac_home_msg);
    }

    private void requestHttp(String url, AjaxParams params, final String type) {
        FinalHttp http = new FinalHttp();
        http.post(url, params, new AjaxCallBack<Object>() {

            @Override
            public void onStart() {
                showProgressDialog();//开启旋转的
                // TODO Auto-generated method stub
                super.onStart();
            }

            @Override
            public void onSuccess(Object t) {
                closeProgressDialog();//关闭旋转的
                try {
                    JSONObject object = new JSONObject(t.toString());
                    //获取数据成功的状态下
                    if (object.getString("ErrorCode").equals(DataCentre.RIGHT_ErrorCode)) {
                        Intent intent = new Intent();
                        intent.putExtra("mobile",mobile);
                        intent.putExtra("carNo",carNo);
                        intent.putExtra("engine_no",engine_no);
                        intent.putExtra("markType",markType);
                        openActivityIntent(CarMessageActivity.class,intent);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                Log.d("OnSuccess", t.toString());
                // TODO Auto-generated method stub
                super.onSuccess(t);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                // TODO Auto-generated method stub
                closeProgressDialog();//关闭旋转的
                super.onFailure(t, errorNo, strMsg);
            }

        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_present:
                if (markType.equals("-1")) {
                    showToastShort("请选择车辆种类");
                    return;
                }
                carNo = et_card_number.getText().toString();
                engine_no = et_engine_number.getText().toString();
                SharedPreferences sharedPreferences = getSharedPreferences("phone", 0);
                mobile = sharedPreferences.getString("phone", "");
                AjaxParams params = new AjaxParams();
                String value = UrlCentre.APPSECRET + "plate_type" + markType + "plate_no" +
                        carNo + "engine_no" + engine_no + "mobile" + mobile +"appkey"+UrlCentre.APPKEY+ UrlCentre.APPSECRET;
                String sign = "";
                try {
                    sign = MD5Util.encodeByMD5(value);
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }
                params.put("appkey", UrlCentre.APPKEY);
                params.put("sign", sign);
                params.put("sign_method", "MD5");
                params.put("timestamp", getNowDate());
                params.put("plate_type",markType);
                params.put("plate_no",carNo);
                params.put("engine_no",engine_no);
                params.put("mobile",mobile);
                requestHttp(UrlCentre.BASEJUNHUAN_URL + UrlCentre.URL_SUBMITCARINFO, params, "add");
                break;
            case R.id.xiaoche:
                if (cst.isShowing()) {
                    cst.dismiss();
                } else {
                    cst.show(dw);
                }
                break;
            default:
                break;
        }
    }

    public static String getNowDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);
        return str;
    }

    @Override
    public void getSeatType(String text) {
        dw.setText(text);
    }
}
